<?php
/**
 * /nmox/modules/Security.class.php
 * Make sure the right people have access
 * 
 * @author David Christian Liedle <david.liedle@gmail.com>
 * @link   http://nmox.org/
 *
 */

class Security {

    var $is_validated = FALSE;
    var $validation_level = 0;
    var $outcome; // result of an attempt
    var $username;
    var $password; // this is the attempt
    var $good_pass; // this is the correct one
    var $clearance;
    var $name_first;
    var $name_last;
    var $name_mid;
    var $nickname;
    var $email_primary;
    
    function Security(){
        // constructor
    }
    
    function getUser($username){
        // retrieve user info
        global $NMOX_CFG;
        if( $NMOX_CFG['MUSE']['Database'] ){
            $query = "SELECT username, password, clearance, name_first, name_last, name_mid, nickname, email_primary ";
            $query .= "FROM users ";
            $query .= "WHERE username='$username'";
            $myUserDB = new Database;
            $myUserDB->connect();
            $myUserDB->add_query("getstuff", $query);
            $myUserDB->do_query("getstuff");
            $myUserDB->load_array("getstuff");
            $info = $myUserDB->get_array("getstuff");
            $myUserDB->close();
            $this->username = $info[0]['username'];
            $this->good_pass = $info[0]['password'];
            $this->clearance = $info[0]['clearance'];
            $this->name_first = $info[0]['name_first'];
            $this->name_last = $info[0]['name_last'];
            $this->name_mid = $info[0]['name_mid'];
            $this->nickname = $info[0]['nickname'];
            $this->email_primary = $info[0]['email_primary'];
        } else {
            // no database module
            die("No database module configured...");
        }
    }
    
    function login_attempt($username, $password){
        $this->getUser($username);
        $this->password = $password;
        if( $this->validateUser() ){
            $this->login();
        } else {
            $this->fail_login();
        }
    }
    
    function validateUser(){
        // match username/password
        if( $this->password === $this->good_pass ){
            $this->is_validated = TRUE;
            $this->outcome = TRUE;
        } else {
            $this->is_validated = FALSE;
            $this->outcome = FALSE;
        }
        return $this->outcome;
    }
    
    function isValid(){
        return $this->is_validated;
    }
    
    function login(){
        // set login
        locateMe("LOGIN SUCCESS CALLED");
    }
    
    function logout(){
        // unset login
        locateMe("LOGOUT SUCCESS CALLED");
    }
    
    function fail_login(){
        // didn't work! now what?
        locateMe("LOGIN FAIL SUCCESSFULLY CALLED");
    }
    
}

locateMe("/nmox/modules/Security.class.php");
